package com.ruoyi.web.controller.pos;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.pos.domain.*;
import com.ruoyi.pos.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Random;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/pos/pos_dingdan")
public class PosDingdanController extends BaseController {
    @Autowired
    private IPosDingdanService posDingdanService;

    @Autowired
    private IPosDingdanInfoService posDingdanInfoService;

    @Autowired
    private IPosCheService posCheService;

    @Autowired
    private IPosDingdanItemService posDingdanItemService;

    @Autowired
    private IPosShangpinService posShangpinService;




//    @GetMapping("/insertPosDingdan")
//    public boolean insertPosDingdan(PosDingdanInfo posDingdanInfo){
//        List<PosDingdanInfo> posDingdanInfoList = posDingdanInfoService.selectPosDingdanInfoList(posDingdanInfo);
//        // 获取当前时间并格式化
//        String now = LocalDateTime.now().format(DATE_TIME_FORMATTER);
//        // 生成四位随机数，不足四位前面补0
//        String randomPart = String.format("%04d", RANDOM.nextInt(10000));
//        // 拼接成最终的订单号
//        String dingdanNumber = now + randomPart;
//        PosDingdan posDingdan = new PosDingdan();
//        posDingdan.setDingdanCard(dingdanNumber);
//        posDingdan.setDingdanInfoId(posDingdanInfoList.get(0).getId());
//        posDingdanService.insertPosDingdan(posDingdan);
//        Integer dingdanId = posDingdanService.getPosDingdanById(dingdanNumber);
//        PosChe posChe = new PosChe();
//        posChe.setCustomerId(posDingdanInfo.getCustomerId());
//        List<PosChe> posCheList = posCheService.selectPosCheList(posChe);
//        List<PosDingdanItem> posDingdanItemList = new ArrayList<>();
//        String[] posCheIdString = posDingdanInfo.getPosCheId().split(",");
//        int[] posCheIdInt = new int[posCheIdString.length];
//        for(int i = 0; i < posCheIdString.length; i++){
//            posCheIdInt[i] = Integer.parseInt(posCheIdString[i]);
//        }
//        for(PosChe pc : posCheList){
//            for(int i = 0; i < posCheIdInt.length; i++){
//                if(pc.getId() == posCheIdInt[i]){
//                    PosDingdanItem posDingdanItem = new PosDingdanItem();
//                    posDingdanItem.setMendianId(pc.getMendianId());
//                    posDingdanItem.setShangpinId(pc.getShangpinId());
//                    posDingdanItem.setColourId(pc.getColourId());
//                    posDingdanItem.setSizeId(pc.getSizeId());
//                    posDingdanItem.setGoodsCount(pc.getGoodsCount());
//                    posDingdanItem.setHarvestWay(pc.getHarvestWay());
//                    posDingdanItem.setDingdanId(dingdanId);
//                    posDingdanItemList.add(posDingdanItem);
//                    break;
//                }
//            }
//
//        }
//        for(PosDingdanItem posDingdanItem : posDingdanItemList){
//            posDingdanItemService.insertPosDingdanItem(posDingdanItem);
//            PosShangpinItem posShangpinItem = new PosShangpinItem();
//            posShangpinItem.setShangpinId(posDingdanItem.getShangpinId());
//            posShangpinItem.setColourId(posDingdanItem.getColourId());
//            posShangpinItem.setSizeId(posDingdanItem.getSizeId());
//            PosShangpinItem posShangpinItem1 = posShangpinService.selectPosShangpinItemList(posShangpinItem).get(0);
//            posShangpinItem1.setGoodsCount(posShangpinItem1.getGoodsCount()-posDingdanItem.getGoodsCount());
//            posShangpinService.updatePosShangpinItem(posShangpinItem1);
//        }
//        for(PosChe posChe1 : posCheList){
//            for(int i = 0; i < posCheIdInt.length; i++){
//                if(posChe1.getId() == posCheIdInt[i]){
//                    posCheService.deletePosCheById(posChe1.getId());
//                    break;
//                }
//            }
//        }
//        return true;
//    }
    @GetMapping("/insertMyPosDingdan")
    public boolean insertMyPosDingdan(PosDingdanInfo posDingdanInfo){
        boolean flag = posDingdanService.insertPosDingDan(posDingdanInfo);
        return flag;
    }

    private static final Random RANDOM = new Random();
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

    /**
     * 生成订单号的方法
     * @return 返回生成的订单号字符串
     */
//    public static String generateOrderNumber() {
//        // 获取当前时间并格式化
//        String now = LocalDateTime.now().format(DATE_TIME_FORMATTER);
//        System.out.println(now);
//        // 生成四位随机数，不足四位前面补0
//        String randomPart = String.format("%04d", RANDOM.nextInt(10000));
//        // 拼接成最终的订单号
//        return now + randomPart;
//    }
//    public static void main(String[] args) {
//        // 测试生成订单号
//        System.out.println("Generated Order Number: " + generateOrderNumber());
//    }
    @GetMapping("/select_pos_dingdan")
    public List<PosDingdan> selectPosDingdan(Integer customerId){
        List<PosDingdan> posDingdanList = posDingdanService.selectMyPosDingDan(customerId);
        for(PosDingdan p : posDingdanList){
            List<PosDingdanItemVO> posDingdanItemVOList = new ArrayList<>();
            PosDingdanItem posDingdanItem = new PosDingdanItem();
            posDingdanItem.setDingdanId(p.getId());
            List<PosDingdanItem> posDingdanItemList = posDingdanItemService.selectPosDingdanItem(posDingdanItem);
            for(PosDingdanItem pdi : posDingdanItemList){
                PosShangpin posShangpin = posShangpinService.selectPosShangpinById(pdi.getShangpinId());
                PosDingdanItemVO posDingdanItemVO = new PosDingdanItemVO(posShangpin,pdi);
                posDingdanItemVOList.add(posDingdanItemVO);
            }
            p.setPosDingdanItemVOList(posDingdanItemVOList);
        }
        return posDingdanList;
    }
    @GetMapping("/dingdan_detail")
    public PosDingdanDetailVO selectposDingdanDetail(Integer id, Integer customerId){
        PosDingdanItem posDingdanItem = posDingdanItemService.selectPosDingdanItemById(id);
        PosShangpin posShangpin = posShangpinService.selectPosShangpinById(posDingdanItem.getShangpinId());
        PosDingdan posDingdan = posDingdanService.selectPosDingdanById(posDingdanItem.getDingdanId());
//        PosDingdanInfo posDingdanInfo1 = new PosDingdanInfo();
//        posDingdanInfo1.setCustomerId(customerId);
//        PosDingdanInfo posDingdanInfo = posDingdanInfoService.selectPosDingdanInfoList(posDingdanInfo1).get(0);
        PosDingdanDetailVO posDingdanDetailVO = new PosDingdanDetailVO(posShangpin,posDingdan,posDingdanItem);
        return posDingdanDetailVO;
    }
    @GetMapping("/delete_pos_dingdan_and_item")
    public int deletePosDingdanAndItem(Integer id,Integer dingdanId){
        int i = posDingdanService.deletePosDingdanAndItem(id,dingdanId);
        return i;
    }
}
